/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.courier.view;

import com.sun.courier.dal.CourierDAL;
import com.sun.courier.entity.Courier;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author NamBH
 */
public class NewJPanel extends javax.swing.JPanel {

    /**
     * Creates new form NewJPanel
     */
    CourierDAL dalCourier = new CourierDAL();
    ArrayList<Courier> lstCourier;

    public NewJPanel() {
        initComponents();
        lstCourier = dalCourier.getAll();
        loadTable();
    }

    private void loadTable() {
        Vector cols = new Vector();
        cols.add("No");
        cols.add("Courier Name");
        cols.add("Customer Name");
        cols.add("Phone");
        cols.add("Status");
        cols.add("Weight");
        cols.add("Depart Location");
        cols.add("Destination");
        cols.add("Distance");

        Vector data = new Vector();
        int i = 1;
        for (Courier item : lstCourier) {
            Vector row = new Vector();
            row.add(i++);
            row.add(item.getCourierName());
            row.add(item.getCustomerName());
            row.add(item.getPhone());
            row.add(item.getStatus());
            row.add(item.getWeight());
            row.add(item.getDepartLocation());
            row.add(item.getDestination());
            row.add(item.getDistance());
            data.add(row);
        }
        DefaultTableModel tblModel = new DefaultTableModel(data, cols);
        tblItem.setModel(tblModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtSearch = new javax.swing.JTextField();
        cbSearch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblItem = new javax.swing.JTable();

        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Courier Name", "Customer Name", "Phone" }));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        tblItem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblItem.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblItem);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        String strSearch = txtSearch.getText();
        if (strSearch.isEmpty()) {
            lstCourier = dalCourier.getAll();
        } else {
            int intCB = cbSearch.getSelectedIndex();
            switch (intCB) {
                case 0:
                    lstCourier = dalCourier.searchCourierName(strSearch);
                    break;
                case 1:
                    lstCourier = dalCourier.searchCustemer(strSearch);
                    break;
                case 2:
                    lstCourier = dalCourier.searchPhone(strSearch);
                    break;
            }
        }
        loadTable();
    }//GEN-LAST:event_btnSearchActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblItem;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
